package com.intelligent.health.gateway.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import reactor.core.publisher.Mono;

@Configuration
public class RedisLimiterConfig {

    @Bean
    @Primary
    public KeyResolver remoteHostLimiterKey() {
        return exchange -> Mono.just(
                exchange.getRequest()
                        .getRemoteAddress()
                        .getAddress()
                        .getHostAddress()
        );
    }

    @Bean("oauthServiceRateLimiter")
    public RedisRateLimiter oauthServiceRateLimiter() {
        return new RedisRateLimiter(100, 100);
    }

    @Bean("memberServiceRateLimiter")
    public RedisRateLimiter memberServiceRateLimiter() {
        return new RedisRateLimiter(50, 50);
    }

    @Bean("setMealServiceRateLimiter")
    public RedisRateLimiter setMealServiceRateLimiter() {
        return new RedisRateLimiter(50, 50);
    }

    @Bean("baseServiceRateLimiter")
    public RedisRateLimiter baseServiceRateLimiter() {
        return new RedisRateLimiter(50, 50);
    }

    @Bean("quartzServiceRateLimiter")
    public RedisRateLimiter quartzServiceRateLimiter() {
        return new RedisRateLimiter(50, 50);
    }

    @Bean("defaultRateLimiter")
    @Primary
    public RedisRateLimiter defaultRateLimiter() {
        return new RedisRateLimiter(50, 100);
    }
}
